<?php
namespace App\Repositories\Admin;

use App\Models\Admin\JobCommentModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\JobStaffModel;
use App\Models\JobsCompanysModel;
use App\Models\User\UserModel;
use App\Repositories\BaseRepository;
use App\Services\Jiguang\JMessageApi;

class JobStaffRepository extends BaseRepository
{
    protected $repository;
    protected $comment;

    public function __construct()
    {
        $this->repository = new JobStaffModel();
        $this->comment = new JobCommentModel();
    }

    /**
     * 操作分页获取
     */
    public function getIndexList()
    {
        $models = $this->repository
            ->orderBy('id','asc')
            ->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 流程中的，通过公司UID
     */
    public function getIndexByCid($uid)
    {
        return $this->repository
            ->where('cid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 流程中的，通过工人UID
     */
    public function getIndexByUid($uid)
    {
        return $this->repository
            ->where('uid',$uid)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    /**
     * 流程中的，通过公司发布的ID
     */
    public function getIndexByCom($com)
    {
        return $this->repository->where('com',$com)
            ->orderBy('id','asc')
            ->skip(0)
            ->take(10)
            ->get();
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$id)
    {
        $data['updated_at'] = time();
        return $this->repository->where('id',$id)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    /**
     * 工人获取投递
     * 通过工人UID、企业UID、岗位ID、招聘类型
     */
    public function getAppApply($conditions)
    {
        $model = $this->repository
            ->where('uid',$conditions['uid'])
            ->where('cid',$conditions['cid'])
            ->where('origin_id',$conditions['jobId'])
//            ->where('from',$conditions['genre'])
            ->first();
        if (!$model) { return ''; }
        return $model;
    }

    /**
     * 工人设置投递
     * 通过工人UID、企业UID、岗位ID、招聘类型
     */
    public function setAppApply($conditions)
    {
        $resModel = $this->getAppApply($conditions);
        if ($resModel) { return $resModel->toAray(); }
        $dataArr = array(
            'uid' => $conditions['uid'],
            'cid' => $conditions['cid'],
            'origin_id' => $conditions['jobId'],
         //   'genre' => $conditions['genre'],
            'created_at' => time(),
            'updated_at' => time(),
        );

        $newid= $this->repository
            ->insertGetId($dataArr);
        $user=UserModel::getinfo($conditions['uid']);
        $job=JobsCompanysModel::getInfo($conditions['jobId']);
        if($user){
            $job=JobModel::getName($job['job']);
            $text='<a class="msgA" href="/h5/my/job/zhaopin_yplist.html?id='.$conditions['jobId'].'&yingpinid='.$newid.'">用户('.$user['name'].')向您发布的('.$job.')岗位投送了简历,请及时查阅</a>';
            JMessageApi::SendMsg($conditions['cid'],$text);
        }

       return $newid;
    }

    /**
     * 工人/企业获取面试
     * 通过工人UID、企业UID、岗位ID、招聘类型
     * @param $uid 工人UID
     * @param $cid 企业UID
     * @param $origin 岗位ID
     * @param $from 类型：1工人投递，2公司应邀
     */
    public function getAppMeet($conditions)
    {
        $model = $this->repository
            ->where('uid',$conditions['uid'])
            ->where('cid',$conditions['cid'])
            ->where('origin_id',$conditions['jobId'])
//            ->where('from',$conditions['genre'])
            ->first();
        if (!$model) { return ''; }
        return $model;
    }

    /**
     * 工人/企业设置面试
     * 通过工人UID、企业UID、岗位ID、招聘类型
     * @param $uid 工人UID
     * @param $cid 企业UID
     * @param $origin 岗位ID
     * @param $from 类型：1工人投递，2公司应邀
     */
    public function setAppMeet($conditions)
    {
        $resModel = $this->getAppMeet($conditions);
        if (!$resModel) { return array(); }
        if ($conditions['genre']==1) {
            //工人投递的，更新状态
            $this->repository
                ->where('id',$resModel->id)
                ->update(['status'=>$conditions['meet']]);
            return $resModel->id;
        } else {
            //公司应邀的，直接创建记录
            $dataArr = array(
                'uid' => $conditions['uid'],
                'cid' => $conditions['cid'],
                'jobId' => $conditions['jobId'],
                'genre' => $conditions['genre'],
                'status' => $conditions['meet'],
                'created_at' => time(),
                'updated_at' => time(),
            );
            return $this->repository
                ->insertGetId($dataArr);
        }
    }

    /**
     * 工人/企业设置流程状态
     */
    public function setAppStatus($id,$status)
    {
        return $this->repository
            ->where('id',$id)
            ->update(['status'=>$status]);
    }

    /**
     * 面试完成度
     */
    public function getSuccessOne($com)
    {
        return $this->repository->where('com',$com)
            ->where('status','>',6)
            ->first();
    }

    public function getStatuss()
    {
        return $this->repository->getStatuss();
    }

    public function getStatusName($status)
    {
        $statuss = $this->repository->getStatuss();
        return array_key_exists($status,$statuss) ?
            $statuss[$status] : '';
    }

    /**
     * 通过用户UID，获取评价
     */
    public function getComment($uid,$recruit)
    {
        $model = $this->comment
            ->where('del',0)
            ->where('uid',$uid)
            ->where('recruit',$recruit)
            ->first();
        return $model ? $model : '';
    }

    /**
     * 设置评价
     * 通过用户UID、招聘类型Recruit、
     * Level评价等级、detail评价内容，
     */
    public function setComment($data)
    {
        $model = $this->getComment($data['uid'],$data['recruit']);
        if ($model) { return ''; }
        return $this->comment
            ->insertGetId(array(
                'uid' => $data['uid'],
                'recruit' => $data['recruit'],
                'level' => $data['level'],
                'detail' => $data['detail'],
                'created_at' => time(),
            ));
    }
}